*** Table 4 ***	

use "$data/simulated_df.dta", clear 

********************************************************************************	
*** Rename some variables for brevity ***
********************************************************************************

cap drop pre_vc
ren (pre_any_vc any_pre_total_fpds_over50k ///
	any_pre_total_patent any_pre_total_allsbir_diff ///
	any_pre_total_hi_cite any_pre_total_hi_orig any_pre_total_app) ///
	(pre_vc pre_fpds pre_patent pre_sbir pre_cite pre_orig pre_app)

cap drop post_vc
ren (post_any_vc1095 any_post_total_fpds_over50k1095 ///
	any_post_total_patent1095 any_post_total_hi_cite1095 ///
	any_post_total_hi_orig1095 any_post_total_allsbir_diff) ///
	(post_vc post_fpds post_patent post_cite post_orig post_sbir)

ren tvar_ac_distance cossim 
ren employees_at_app employees

* Labels
lab var num_per_topic "Num Proposals per Topic"
lab var winners_per_topic "Num Winners per Topic"
lab var age "Age"
lab var employees "Number of Employees"
lab var in_vchub "$\mathbbm{1}$(in VC Hub)"
lab var in_afbase_city "$\mathbbm{1}$(in County with AF Base)"
lab var minority "$\mathbbm{1}$(Minority Owned)"
lab var woman_own "$\mathbbm{1}$(Woman owned)"
lab var pre_vc "$\mathbbm{1}$(Previous VC)"
lab var pre_fpds "$\mathbbm{1}$(Previous DoD Contract)"
lab var pre_patent "$\mathbbm{1}$(Previous Patent)"
lab var pre_sbir "$\mathbbm{1}$(Previous SBIR Contract)"
lab var pre_orig "$\mathbbm{1}$(Previous High Originality Patent)"
lab var post_vc "$\mathbbm{1}$(VC)"
lab var post_fpds "$\mathbbm{1}$(DoD Contract)"
lab var post_patent "$\mathbbm{1}$(Patent)"
lab var post_sbir "$\mathbbm{1}$($\geq 2$ SBIR)"
lab var post_orig "$\mathbbm{1}$(High Originality Patent)"
lab var pre_cite "$\mathbbm{1}$(Previous High Citation Patent)"
lab var pre_app "$\mathbbm{1}$(Previous Patent Application)"
lab var post_cite "$\mathbbm{1}$(High Citation Patent)"
lab var software "$\mathbbm{1}$(Software)"
lab var foreign_national_flag "$\mathbbm{1}$(Immigrant)"
lab var rank_v2 "Normalized Rank"
lab var cossim "Non-specificity Index"

********************************************************************************
*** Get summary stats ***
********************************************************************************

foreach var of varlist in_vchub in_afbase_city age ///
	employees foreign_national_flag minority woman_own ///
	pre_fpds pre_vc pre_patent pre_orig pre_cite pre_sbir ///
	num_per_topic frac_win winners_per_topic cossim { 
		
		local l`var': variable label `var'
		
		cap drop one_per_topic
		bys topic_n phase: gen one_per_topic = _n 
		
		bys topic_n phase: egen `var'm = mean(`var')
		ren `var' `var'_orig 
		ren `var'm `var'
		
		local oneper "& one_per_topic==1 & winners_per_topic!=0" 
		local clus "topic_n"
	
		foreach x in 0 1 {
			qui sum `var' if $conv_all & non_specific==`x' `oneper', det
			local `var'_`x'n: di %10.0fc `r(N)'
			local `var'_`x'mu: di %10.3fc `r(mean)'
			local `var'_`x'sd: di %10.3fc `r(sd)'
			local `var'_`x'p50: di %10.3fc `r(p50)'
			local `var'_`x'min: di %10.3fc `r(min)'
			local `var'_`x'max: di %10.3fc `r(max)'
		}
		
		qui ttest `var' if $conv_all `oneper', by(non_specific)
		local `var'_d = `r(mu_2)' - `r(mu_1)' 
		local `var'_d: di %10.3f ``var'_d'
		qui reg `var' i.non_specific if $conv_all `oneper', cluster(`clus')
		local t = _b[1.non_specific]/_se[1.non_specific]
		local p =2*ttail(e(df_r),abs(`t'))
		
		if `p' <= 0.01 {
			local `var'_d "``var'_d'$^{***}$"
		}
		if `p' > 0.01 & `p' <= 0.05 {
			local `var'_d "``var'_d'$^{**}$"
		}
		if `p' > 0.05 & `p' <= 0.1 {
			local `var'_d "``var'_d'$^*$"
		}
		
		local `var'_p : di %10.3f `p'

	}

* Nonspecific 
cap file close des
file open des using "$tables/table4.tex", write replace
file write des
file write des "\begin{footnotesize}" _n
file write des "\begin{tabular}{lccccccc}" _n
file write des " \toprule \tabularnewline" _n
file write des "& \multicolumn{3}{c}{$ \leq $ P66 Non-Specificity} & \multicolumn{3}{c}{$>$ P66 Non-Specificity} \tabularnewline" _n
file write des "\cmidrule(lr){2-4} \cmidrule(lr){5-7} " _n
file write des " & \multirow{2}{*}{N} & \multirow{2}{*}{Mean} & \multirow{2}{*}{SD} & \multirow{2}{*}{N} & \multirow{2}{*}{Mean} & \multirow{2}{*}{SD} & Diff of \tabularnewline" _n
file write des " & & &  & & & & Means \tabularnewline" _n
file write des "\midrule" _n

* Fill in locals
file write des "\multicolumn{8}{l}{\bf Company Characteristics} \tabularnewline" _n
file write des "\multicolumn{8}{l}{\it Lifecycle controls} \tabularnewline" _n
foreach var of varlist age employees {
	file write des "`l`var'' & ``var'_0n' & ``var'_0mu'  & ``var'_0sd' & ``var'_1n' & ``var'_1mu' & ``var'_1sd' & ``var'_d'  \tabularnewline" _n
}

file write des " \tabularnewline" _n
file write des "\multicolumn{9}{l}{\it Additional controls} \tabularnewline" _n
foreach var of varlist  in_vchub in_afbase_city ///
	foreign_national_flag minority woman_own  {
		file write des "`l`var'' & ``var'_0n' & ``var'_0mu'  & ``var'_0sd' & ``var'_1n' & ``var'_1mu' & ``var'_1sd' & ``var'_d'  \tabularnewline" _n
	}

file write des " \tabularnewline" _n
file write des "\multicolumn{8}{l}{\bf Pre-Award Outcomes} \tabularnewline" _n

foreach var of varlist pre_fpds pre_vc pre_patent pre_orig pre_cite pre_sbir {
		file write des "`l`var'' & ``var'_0n' & ``var'_0mu' &``var'_0sd' & ``var'_1n' & ``var'_1mu' & ``var'_1sd' &``var'_d' \tabularnewline" _n
	}
	
	
file write des "  \tabularnewline" _n
file write des "\multicolumn{8}{l}{\bf Competition Characteristics} \tabularnewline" _n
//dropped age employees
foreach var of varlist  num_per_topic winners_per_topic cossim  {
	file write des "`l`var'' & ``var'_0n' & ``var'_0mu'  & ``var'_0sd' & ``var'_1n' & ``var'_1mu' & ``var'_1sd' & ``var'_d'  \tabularnewline" _n
}

* Table footer
{
file write des " \bottomrule" _n
file write des " & & &  & & & & & \tabularnewline" _n
file write des "\end{tabular}" _n
file write des "\end{footnotesize}" _n
}
file close des
di "Done!"
